﻿package com.badibadi.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alipay.android.msp.Keys;
import com.alipay.android.msp.Result;
import com.alipay.android.msp.SignUtils;
import com.alipay.sdk.app.PayTask;
import com.badibadi.infos.Activity_Overview_Model;
import com.badibadi.infos.CostDataModel;
import com.badibadi.infos.GmcModel;
import com.badibadi.infos.Results;
import com.badibadi.mail.Mail13_6Activity;
import com.badibadi.mytools.CalendarTools;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;
import com.libjph.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.my_view.MyListView_ShowAll;
import com.view.my_view.XListView;
import com.view.my_view.XListView.IXListViewListener;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 金币充值（购买处）创建时间：2014-10-10
 * 
 * @author Administrator
 * 
 */
public class RechargeActivity extends BaseActivity implements OnClickListener {
	private static final int SDK_PAY_FLAG = 1;

	private static final int SDK_CHECK_FLAG = 2;

	protected static final String TAG = "RechargeActivity";
	private MyListView_ShowAll xListView1;
	private RechargeAdapter adapter;
	private EditText recharge_jinbi_num;
	private int Page = 1;// 当前显示数据页数
	private int pageNum = 10;
	private Results results, results2;
	public String Uid;

	// 网络数据
	private GmcModel gmcModel;
	private DisplayImageOptions options;
	private List<CostDataModel> gmclist;
	private LinearLayout JoinClub, JoinActivity, JoinRecord, JoinCousult;
	private String nummm;// 充值金币数量
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				try {
					Utils.ExitPrgress(RechargeActivity.this);
					Utils.showMessage(RechargeActivity.this, getResources()
							.getString(R.string.l_net_error));
				} catch (Exception e) {
				}
				// xListView1.stopRefresh();
				// xListView1.stopLoadMore();
				break;
			case 2:
				Utils.ExitPrgress(RechargeActivity.this);
				try {
					if (gmcModel.getCostData() != null)
						gmclist = gmcModel.getCostData();
					initView();
				} catch (Exception e) {
					// TODO: handle exception
				}

				// activity_Overview_Models.addAll(list);
				// adapter.notifyDataSetChanged();
				// xListView1.stopRefresh();
				// xListView1.stopLoadMore();
				break;
			case 3:
				try {
					Utils.ExitPrgress(RechargeActivity.this);
					Utils.showMessage(RechargeActivity.this, getResources()
							.getString(R.string.l_xa10));
				} catch (Exception e) {
				}
				// xListView1.stopRefresh();
				// xListView1.stopLoadMore();
				break;

			case RQF_PAY:
				break;

			case RQF_LOGIN: {
				// Result result = new Result((String) msg.obj);
				// Toast.makeText(RechargeActivity.this, result.getResult(),
				// Toast.LENGTH_SHORT).show();
			}

				// xListView1.stopRefresh();
				// xListView1.stopLoadMore();
				break;
			case 6:
				try {
					Utils.ExitPrgress(RechargeActivity.this);
					// Utils.showMessage(RechargeActivity.this,
					// results2.getRetmsg());

					if (results2.isRet()) {

						try {
							// String num = recharge_jinbi_num.getText()
							// .toString().trim();
							Double d = Double.parseDouble(nummm)
									* Double.parseDouble(gmcModel
											.getGold_to_RMB());
							// String info = getNewOrderInfo(
							// results2.getRetmsg(),
							// d + "",
							// getResources().getString(R.string.l_xb170)
							// + nummm
							// + getResources().getString(
							// R.string.l_xb171));
							String info = getOrderInfo(results2.getRetmsg(),
									getResources().getString(R.string.l_xb171),
									("" + d),
									getResources().getString(R.string.l_xb170)
											+ nummm);
							String sign = sign(info);
							try {
								// 仅需对sign 做URL编码
								sign = URLEncoder.encode(sign, "UTF-8");
							} catch (UnsupportedEncodingException e) {

							}
							final String payInfo = info + "&sign=\"" + sign
									+ "\"&" + getSignType();

							//
							Runnable payRunnable = new Runnable() {

								@Override
								public void run() {
									// 构造PayTask 对象
									PayTask alipay = new PayTask(
											RechargeActivity.this);
									// 调用支付接口
									String result = alipay.pay(payInfo);

									Message msg = new Message();
									//
									msg.what = SDK_PAY_FLAG;
									msg.obj = result;
									mHandler2sd.sendMessage(msg);
								}
							};

							Thread payThread = new Thread(payRunnable);
							payThread.start();
						} catch (Exception ex) {

							Toast.makeText(RechargeActivity.this,
									R.string.remote_call_failed,
									Toast.LENGTH_SHORT).show();
						}

					}
				} catch (Exception e) {
				}
				break;
			default:
				break;
			}
		}
	};

	private Handler mHandler2sd = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SDK_PAY_FLAG: {
				Result resultObj = new Result((String) msg.obj);
				String resultStatus = resultObj.resultStatus;

				// 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
				if (TextUtils.equals(resultStatus, "9000")) {
					Toast.makeText(RechargeActivity.this, "支付成功",
							Toast.LENGTH_SHORT).show();
					RechargeActivity.this.finish();
				} else {
					// 判断resultStatus 为非“9000”则代表可能支付失败
					// “8000”
					// 代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
					if (TextUtils.equals(resultStatus, "8000")) {
						Toast.makeText(RechargeActivity.this, "支付结果确认中",
								Toast.LENGTH_SHORT).show();

					} else {
						Toast.makeText(RechargeActivity.this, "支付失败",
								Toast.LENGTH_SHORT).show();
					}
				}
				break;
			}
			case SDK_CHECK_FLAG: {
				Toast.makeText(RechargeActivity.this, "检查结果为：" + msg.obj,
						Toast.LENGTH_SHORT).show();
				break;
			}
			default:
				break;
			}
		};
	};

	// 支付宝的
	private static final int RQF_PAY = 4;

	private static final int RQF_LOGIN = 5;

	// DefaultAlipayClient defaultAlipayClient;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.recharge);
		// 链接到支付宝
		this.findViewById(R.id.recharge).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						nummm = "";
						nummm = recharge_jinbi_num.getText().toString().trim();
						if (!StringUtil.isNullOrEmpty(nummm)) {
							// gold(num);
							if (gmcModel != null) {
								Getgold(nummm, gmcModel.getGold_to_RMB());
								// PayUtils.gold(RechargeActivity.this, num,
								// gmcModel.getGold_to_RMB());
							}

						} else {
							Utils.showMessage(RechargeActivity.this, "请填写金币数额");
						}

					}
				});

		this.findViewById(R.id.return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						finish();
					}
				});
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
		Uid = Utils.getUid(RechargeActivity.this);
		String uid = Utils.getUid(RechargeActivity.this);
		System.out.println("uid" + uid);

		JoinClub = (LinearLayout) this.findViewById(R.id.JoinClub);
		JoinActivity = (LinearLayout) this.findViewById(R.id.JoinActivity);
		JoinRecord = (LinearLayout) this.findViewById(R.id.JoinRecord);
		JoinCousult = (LinearLayout) this.findViewById(R.id.JoinConcult);
		recharge_jinbi_num = (EditText) this
				.findViewById(R.id.recharge_jinbi_num);
		JoinClub.setOnClickListener(this);
		JoinActivity.setOnClickListener(this);
		JoinRecord.setOnClickListener(this);
		JoinCousult.setOnClickListener(this);

		this.findViewById(R.id.before_index).setOnClickListener(this);
		gmc(uid, Page, pageNum);
	}

	/**
	 * sign the order info. 对订单信息进行签名
	 * 
	 * @param content
	 *            待签名订单信息
	 */
	public String sign(String content) {
		return SignUtils.sign(content, Keys.RSA_PRIVATE);
	}

	/**
	 * get the sign type we use. 获取签名方式
	 * 
	 */
	public String getSignType() {
		return "sign_type=\"RSA\"";
	}

	private void Getgold(final String price, final String dj) {
		Utils.showPrgress(RechargeActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(RechargeActivity.this));
				map.put("price", price);
				//
				String actcontent = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "order/gold");
				if (actcontent != null) {
					//
					results2 = Utils.checkResult_NNN(getApplicationContext(),
							actcontent);
					if (results2 != null
							&& !results2.getRetmsg().equals("null")) {
						//
						mHandler.sendEmptyMessage(6);
					} else {
						mHandler.sendEmptyMessage(3);
					}
				} else {
					mHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	/**
	 * 购买处---POST
	 * 
	 * @param $uid
	 *            //登录用户ID
	 * @param $page
	 *            ; //第几页
	 * @param $pageNum
	 *            ;//每页显示条数
	 */
	public void gmc(final String uid, final int page, final int pageNum) {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				// list = new ArrayList<Activity_Overview_Model>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid);
				map.put("page", "" + page);
				map.put("pageNum", "" + pageNum);
				String actcontent = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "user/gmc");
				if (actcontent != null) {
					//
					results = Utils.checkResult_NNN(RechargeActivity.this,
							actcontent);
					System.out.println("gmc" + actcontent);
					if (results != null && !results.getRetmsg().equals("null")) {
						//
						try {
							gmcModel = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), GmcModel.class);
							System.out.println("gmcshuju" + results.getRetmsg());
							mHandler.sendEmptyMessage(2);
						} catch (Exception e) {

							mHandler.sendEmptyMessage(3);
						}
					} else {
						mHandler.sendEmptyMessage(3);
					}
				} else {
					mHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	/**
	 * 
	 * @param price
	 */
	private void gold(final String price) {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				// list = new ArrayList<Activity_Overview_Model>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", Utils.getUid(RechargeActivity.this));
				map.put("price", price);
				//
				String actcontent = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "order/gold");
				if (actcontent != null) {
					//
					// results2 = Utils.checkResult_NNN(RechargeActivity.this,
					// actcontent);
					// if (results2 != null
					// && !results2.getRetmsg().equals("null")) {
					//
					// mHandler.sendEmptyMessage(6);
					// } else {
					// mHandler.sendEmptyMessage(3);
					// }
				} else {
					mHandler.sendEmptyMessage(1);
				}
			}
		});
	}

	private void initView() {
		// 头像
		ImageView head = (ImageView) this.findViewById(R.id.head);
		try {
			ImageLoader.getInstance().displayImage(
					Constants.BadiDownImgUrl + "/" + gmcModel.getHead()
							+ Constants.App9block, head, options);
		} catch (Exception e) {
			// TODO: handle exception
		}
		// 昵称
		TextView nickName = (TextView) this.findViewById(R.id.nickName);
		nickName.setText("" + gmcModel.getNickName());
		// e_mail
		TextView e_mail = (TextView) this.findViewById(R.id.e_mail);
		e_mail.setText("" + gmcModel.getEmail());
		// phoneNumber
		TextView phoneNumber = (TextView) this.findViewById(R.id.phoneNumber);

		if (gmcModel.getPhone() != null) {
			if (gmcModel.getPhone().length() > 7) {
				String a = gmcModel.getPhone().substring(0, 3);
				String b = gmcModel.getPhone().substring(
						gmcModel.getPhone().length() - 4,
						gmcModel.getPhone().length());
				phoneNumber.setText(a + "****" + b);
			} else {
				String a = gmcModel.getPhone().substring(0, 1);
				String b = gmcModel.getPhone().substring(
						gmcModel.getPhone().length() - 1,
						gmcModel.getPhone().length());
				phoneNumber.setText(a + "****" + b);
			}
		}

		// gold 余额
		TextView gold = (TextView) this.findViewById(R.id.gold);
		gold.setText("" + gmcModel.getGold());
		// goldtormb
		TextView gold_to_RMB = (TextView) this.findViewById(R.id.gold_to_RMB);
		gold_to_RMB.setText("" + gmcModel.getGold_to_RMB());
		//
		// xListView1 = (ListView) this.findViewById(R.id.RechargeListView);
		// adapter = new RechargeAdapter(RechargeActivity.this, gmclist);
		// xListView1.setAdapter(adapter);
		xListView1 = (MyListView_ShowAll) this
				.findViewById(R.id.RechargeListView);
		adapter = new RechargeAdapter(RechargeActivity.this, gmclist);
		xListView1.setAdapter(adapter);
		setListViewHeightBasedOnChildren(xListView1);
		xListView1.setPadding(10, 10, 10, 10);
		// xListView1.setPullLoadEnable(false);
		// xListView1.setPullRefreshEnable(false);

		// xListView1.startRefresh();
		// xListView1.startLoadMore();

		// final String uid = Utils.getUid(RechargeActivity.this);
		// 改 显示加载的内容
		final Button shangyiye = (Button) findViewById(R.id.shangyiye);
		final Button xiayiye = (Button) findViewById(R.id.xiayiye);
		shangyiye.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (Page != 1) {
					gmclist.clear();
					Page--;
					String uid = Utils.getUid(RechargeActivity.this);
					gmc(uid, Page, pageNum);
					xListView1.setAdapter(adapter);
					setListViewHeightBasedOnChildren(xListView1);
					xiayiye.setBackgroundResource(R.drawable.play_f);
					adapter.notifyDataSetChanged();
				} else if (Page == 1) {
					shangyiye.setBackgroundResource(R.drawable.play_2);
				}
			}
		});
		xiayiye.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (Integer.parseInt(gmcModel.getCount()) % pageNum != 0) {
					if (Page < (Integer.parseInt(gmcModel.getCount()) / pageNum) + 1) {
						gmclist.clear();
						shangyiye.setBackgroundResource(R.drawable.play_1);
						Page++;
						String uid = Utils.getUid(RechargeActivity.this);
						gmc(uid, Page, pageNum);
						xListView1.setAdapter(adapter);
						setListViewHeightBasedOnChildren(xListView1);
						adapter.notifyDataSetChanged();
					} else if (Page == ((Integer.parseInt(gmcModel.getCount())) / pageNum) + 1) {
						xiayiye.setBackgroundResource(R.drawable.play_2_f);
					}

				} else {
					if (Page < (Integer.parseInt(gmcModel.getCount()) / pageNum)) {
						gmclist.clear();
						shangyiye.setBackgroundResource(R.drawable.play_1);
						Page++;
						String uid = Utils.getUid(RechargeActivity.this);
						gmc(uid, Page, pageNum);
						xListView1.setAdapter(adapter);
						setListViewHeightBasedOnChildren(xListView1);
						adapter.notifyDataSetChanged();
					} else if (Page == ((Integer.parseInt(gmcModel.getCount())) / pageNum)) {
						xiayiye.setBackgroundResource(R.drawable.play_2_f);
					}
				}
			}
		});

	}

	/**
	 * 动态设置ListView的高度
	 * 
	 * @param listView
	 */
	public static void setListViewHeightBasedOnChildren(ListView listView) {

		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {

			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));

		listView.setLayoutParams(params);
	}

	// TODO Auto-generated method stub
	class RechargeAdapter extends BaseAdapter {
		Context context;
		// 交易
		// private String[] trading;
		// 时间
		// private String[] time;

		// private String[] money;

		List<CostDataModel> costDatalist;
		/**
		 * 因为有三种交易记录
		 */
		private int[] type;

		public RechargeAdapter(Context ct, List<CostDataModel> mlist) {
			context = ct;
			costDatalist = mlist;

			// trading=new String[]{
			// "音乐联盟","测试会","sdf","gf","sfs","sss"
			// };
			// time=new String[]{
			// "2014-4-7","2014-4-7  12:00","2014-4-7","2014-4-7","2014-4-8","2014-4-7"
			// };
			// money=new String[]{
			// "1","2","3","44","5","2"
			// };
			// type=new int[]{
			// 0,2,1,2,1,0
			// };

		}

		@Override
		public int getItemViewType(int position) {
			// 消费类别 join_activity:参加活动 attend_club：参加俱乐部 record_vote：记录投票
			// consult_comment:评价
			if (costDatalist.get(position).getType().equals("attend_club")) {
				return 0;
			} else if (costDatalist.get(position).getType()
					.equals("join_activity")) {
				return 1;
			} else if (costDatalist.get(position).getType()
					.equals("record_vote")) {
				return 2;
			} else if (costDatalist.get(position).getType()
					.equals("consult_comment")) {
				return 3;
			}
			return 0;
		}

		@Override
		public int getViewTypeCount() {
			return 4;
		}

		@Override
		public int getCount() {
			if (costDatalist != null) {
				return costDatalist.size();
			} else {
				return 0;
			}

		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		// status
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			int Type = getItemViewType(position);
			if (convertView == null) {
				holder = new ViewHolder();
				LayoutInflater mInflater = LayoutInflater
						.from(RechargeActivity.this);
				convertView = mInflater.inflate(R.layout.recharge_adpter_item,
						null);
				holder.club = (TextView) convertView
						.findViewById(R.id.joinclub);
				holder.chenggong = (TextView) convertView
						.findViewById(R.id.xianshichenggong);
				holder.ticheng = (TextView) convertView
						.findViewById(R.id.xianshiticheng);
				holder.jinbi = (TextView) convertView
						.findViewById(R.id.xianshijinbi);
				holder.danwei = (TextView) convertView
						.findViewById(R.id.xianshishuliang);
				holder.money = (TextView) convertView.findViewById(R.id.money);
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.status = (TextView) convertView
						.findViewById(R.id.status);
				convertView.setTag(holder);
			} else if (convertView != null) {
				holder = (ViewHolder) convertView.getTag();
			}
			switch (Type) {
			case 0:// 参加俱乐部
				if (costDatalist.get(position).getNames() != null) {
					String shifouchouqu = null;
					String shifouSuSuccess = null;
					String uid = Utils.getUid(RechargeActivity.this);
					if (costDatalist.get(position).getStatus().equals("1")) {
						holder.chenggong.setText(context.getResources()
								.getString(R.string.Success_gmc));
						System.out.println("Uid" + uid + "id"
								+ costDatalist.get(position).getAdmin_id());
						if (uid.equals(costDatalist.get(position).getAdmin_id())) {
							holder.ticheng.setText("("
									+ context.getResources().getString(
											R.string.bonus));
							holder.jinbi.setText(costDatalist.get(position)
									.getAdmin_gold());
							holder.danwei.setText(context.getResources()
									.getString(R.string.GeMoneyNum) + ")");

						}

					} else {
						holder.chenggong.setText(context.getResources()
								.getString(R.string.Failed_gmc));

					}

					String text = String.format(
							costDatalist.get(position).getNickName()
									+ context.getResources().getString(
											R.string.JoinXXClub), costDatalist
									.get(position).getNames());
					int index = text.indexOf(costDatalist.get(position)
							.getNickName());
					int indexone = text.indexOf(costDatalist.get(position)
							.getNames());
					SpannableStringBuilder stBuilder = new SpannableStringBuilder(
							text);
					stBuilder.setSpan(new ForegroundColorSpan(context
							.getResources().getColor(R.color.blue)), index,
							(index + costDatalist.get(position).getNickName()
									.length()),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

					stBuilder.setSpan(new ForegroundColorSpan(context
							.getResources().getColor(R.color.blue)), indexone,
							(indexone + costDatalist.get(position).getNames()
									.length()),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					stBuilder.setSpan(
							new Clickable(context, costDatalist.get(position)
									.getUid(), 2), index,
							costDatalist.get(position).getNickName().length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					stBuilder.setSpan(
							new Clickable(context, costDatalist.get(position)
									.getCosttype_id(), 1), indexone, indexone
									+ costDatalist.get(position).getNames()
											.length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					holder.club.setText(stBuilder);
					holder.club.setMovementMethod(LinkMovementMethod
							.getInstance());

				}
				break;
			case 1:// 参加活动
					// 这里

				if (costDatalist.get(position).getNames() != null) {

					String shifouchouqu = null;
					String shifouSuSuccess = "shifouSuSuccess";
					String uid = Utils.getUid(RechargeActivity.this);
					if (costDatalist.get(position).getStatus().equals("1")) {
						holder.chenggong.setText(context.getResources()
								.getString(R.string.Success_gmc));
						if (uid.equals(costDatalist.get(position).getAdmin_id())) {
							holder.ticheng.setText("("
									+ context.getResources().getString(
											R.string.bonus));
							holder.jinbi.setText(costDatalist.get(position)
									.getAdmin_gold());
							holder.danwei.setText(context.getResources()
									.getString(R.string.GeMoneyNum) + ")");
						}
					} else {
						holder.chenggong.setText(context.getResources()
								.getString(R.string.Failed_gmc));
						shifouchouqu = "";
					}

					String text1 = String.format(
							costDatalist.get(position).getNickName()
									+ context.getResources().getString(
											R.string.JoinXXActivity),
							costDatalist.get(position).getNames());
					int index = text1.indexOf(costDatalist.get(position)
							.getNickName());
					int indexone = text1.indexOf(costDatalist.get(position)
							.getNames());
					SpannableStringBuilder stBuilder1 = new SpannableStringBuilder(
							text1);
					stBuilder1.setSpan(new ForegroundColorSpan(context
							.getResources().getColor(R.color.blue)), index,
							(index + costDatalist.get(position).getNickName()
									.length()),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

					stBuilder1.setSpan(new ForegroundColorSpan(context
							.getResources().getColor(R.color.blue)), indexone,
							(indexone + costDatalist.get(position).getNames()
									.length()),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					stBuilder1.setSpan(
							new Clickable(context, costDatalist.get(position)
									.getUid(), 2), index,
							costDatalist.get(position).getNickName().length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

					stBuilder1.setSpan(
							new Clickable(context, costDatalist.get(position)
									.getCosttype_id(), 3), indexone, indexone
									+ costDatalist.get(position).getNames()
											.length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					Uid = costDatalist.get(position).getUid();
					holder.club.setText(stBuilder1);
					holder.club.setMovementMethod(LinkMovementMethod
							.getInstance());

				}

				break;
			case 2:// 发起记录投票

				if (costDatalist.get(position).getNames() != null) {
					String text2 = String.format(
							costDatalist.get(position).getNickName()
									+ context.getResources().getString(
											R.string.JoinXXRecord),
							costDatalist.get(position).getNames());
					int index = text2.indexOf(costDatalist.get(position)
							.getNickName());
					int indexone = text2.indexOf(costDatalist.get(position)
							.getNames());
					SpannableStringBuilder stBuilder2 = new SpannableStringBuilder(
							text2);
					stBuilder2.setSpan(new ForegroundColorSpan(context
							.getResources().getColor(R.color.blue)), index,
							(index + costDatalist.get(position).getNickName()
									.length()),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					stBuilder2.setSpan(new ForegroundColorSpan(context
							.getResources().getColor(R.color.blue)), indexone,
							indexone
									+ costDatalist.get(position).getNames()
											.length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					stBuilder2.setSpan(
							new Clickable(context, costDatalist.get(position)
									.getUid(), 2), index,
							costDatalist.get(position).getNickName().length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					stBuilder2.setSpan(
							new Clickable(context, costDatalist.get(position)
									.getUid(), 2), indexone, indexone
									+ costDatalist.get(position).getNames()
											.length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					holder.club.setText(stBuilder2);
					holder.club.setMovementMethod(LinkMovementMethod
							.getInstance());
				}
				break;
			case 3:
				if (costDatalist.get(position).getRe_nickName() != null) {

					String shifouchouqu = null;
					String shifouSuSuccess = "shifouSuSuccess";
					if (costDatalist.get(position).getStatus().equals("1")) {
						holder.chenggong.setText(context.getResources()
								.getString(R.string.Success_gmc)
								+ "("
								+ context.getResources().getString(
										R.string.bonus));
						holder.jinbi.setText(costDatalist.get(position)
								.getCost_gold());
						holder.danwei.setText(context.getResources().getString(
								R.string.GeMoneyNum)
								+ ")");

					} else {
						holder.chenggong.setText(context.getResources()
								.getString(R.string.Failed_gmc));
						shifouchouqu = "";
					}

					String text3 = String.format(costDatalist.get(position)
							.getNickName()
							+ context.getResources().getString(
									R.string.wx_txt_7)
							+ costDatalist.get(position).getRe_nickName());
					int index = text3.indexOf(costDatalist.get(position)
							.getNickName());
					int indexone = text3.indexOf(costDatalist.get(position)
							.getRe_nickName());
					SpannableStringBuilder stBuilder1 = new SpannableStringBuilder(
							text3);
					stBuilder1.setSpan(new ForegroundColorSpan(context
							.getResources().getColor(R.color.blue)), index,
							(index + costDatalist.get(position).getNickName()
									.length()),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					System.out.println(costDatalist.get(position)
							.getRe_nickName());
					System.out.println(indexone);

					stBuilder1.setSpan(new ForegroundColorSpan(context
							.getResources().getColor(R.color.blue)), indexone,
							(indexone + costDatalist.get(position)
									.getRe_nickName().length()),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					stBuilder1.setSpan(
							new Clickable(context, costDatalist.get(position)
									.getUid(), 2), index,
							costDatalist.get(position).getNickName().length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

					stBuilder1.setSpan(
							new Clickable(context, costDatalist.get(position)
									.getRe_uid(), 2), indexone, indexone
									+ costDatalist.get(position)
											.getRe_nickName().length(),
							Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
					Uid = costDatalist.get(position).getUid();
					holder.club.setText(stBuilder1);
					holder.club.setMovementMethod(LinkMovementMethod
							.getInstance());

				}
				break;
			}
			// 花费金币数
			holder.money
					.setText("" + costDatalist.get(position).getCost_gold());
			String times = null;
			try {
				times = CalendarTools.getFormatTime(
						Double.parseDouble(costDatalist.get(position)
								.getCost_time() + "000"), "yyyy.MM.dd HH:mm");
			} catch (Exception e) {
				// TODO: handle exception
			}

			holder.time.setText("" + times);// 花费时间

			if (costDatalist.get(position).getStatus().equals("0")) {
				holder.status.setText(""
						+ context.getResources().getString(R.string.wx2_32));// 交易状态
			} else if (costDatalist.get(position).getStatus().equals("1")) {
				holder.status.setText(""
						+ context.getResources().getString(
								R.string.ASuccessfulDeal));// 交易状态
			} else if (costDatalist.get(position).getStatus().equals("2")) {
				holder.status.setText(""
						+ context.getResources().getString(R.string.wx2_33));// 交易状态
			}

			// 当为1时显示“交易成功”，当为0显示“交易失败”，当为2时显示“交易处理中”
			return convertView;
		}

		public final class ViewHolder {
			public TextView status;
			public TextView club;
			public TextView money;
			public TextView time;
			public TextView chenggong;
			public TextView ticheng;
			public TextView jinbi;
			public TextView danwei;
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.JoinClub:
			Intent intent = new Intent(RechargeActivity.this,
					ClubListActivity.class);
			startActivity(intent);
			break;
		case R.id.JoinActivity:
			Intent intent1 = new Intent(RechargeActivity.this,
					ActivityOverViewActivity.class);
			startActivity(intent1);
			break;
		case R.id.JoinRecord:
			Intent intent2 = new Intent(RechargeActivity.this,
					RecordAdayToRememberActivity.class);
			startActivity(intent2);
			break;
		case R.id.JoinConcult:
			Intent intent4 = new Intent(RechargeActivity.this,
					AboutConsultingFeesActivity.class);
			startActivity(intent4);
			break;
		case R.id.before_index:
			Intent intent3 = new Intent(RechargeActivity.this,
					IndexActivity.class);
			startActivity(intent3);
			break;
		}
	}

	/**
	 * create the order info. 创建订单信息
	 * 
	 */
	public String getOrderInfo(String dingdanhao, String body, String price,
			String info) {
		// 合作者身份ID
		String orderInfo = "partner=" + "\"" + Keys.DEFAULT_PARTNER + "\"";

		// 卖家支付宝账号
		orderInfo += "&seller_id=" + "\"" + Keys.DEFAULT_SELLER + "\"";

		// 商户网站唯一订单号
		orderInfo += "&out_trade_no=" + "\"" + dingdanhao + "\"";

		// 商品名称
		orderInfo += "&subject=" + "\"" + info + "\"";

		// 商品详情
		orderInfo += "&body=" + "\"" + body + "\"";

		// 商品金额
		orderInfo += "&total_fee=" + "\"" + price + "\"";

		// 服务器异步通知页面路径
		orderInfo += "&notify_url=" + "\""
				+ "http://www.uniclubber.com/App/PayGold/notify.html" + "\"";

		// 接口名称， 固定值
		orderInfo += "&service=\"mobile.securitypay.pay\"";

		// 支付类型， 固定值
		orderInfo += "&payment_type=\"1\"";

		// 参数编码， 固定值
		orderInfo += "&_input_charset=\"utf-8\"";

		// 设置未付款交易的超时时间
		// 默认30分钟，一旦超时，该笔交易就会自动被关闭。
		// 取值范围：1m～15d。
		// m-分钟，h-小时，d-天，1c-当天（无论交易何时创建，都在0点关闭）。
		// 该参数数值不接受小数点，如1.5h，可转换为90m。
		orderInfo += "&it_b_pay=\"30m\"";

		// 支付宝处理完请求后，当前页面跳转到商户指定页面的路径，可空
		orderInfo += "&return_url=\"m.alipay.com\"";

		// 调用银行卡支付，需配置此参数，参与签名， 固定值
		// orderInfo += "&paymethod=\"expressGateway\"";

		return orderInfo;
	}

	class Clickable extends ClickableSpan implements OnClickListener {
		public String id;
		public int type;
		public Context context;

		public Clickable(Context context, String id, int type) {
			this.context = context;
			this.id = id;
			this.type = type;
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			// TODO Auto-generated method stub
			super.updateDrawState(ds);
			ds.setUnderlineText(false); // 去掉下划线
		}

		@Override
		public void onClick(View v) {

			if (type == 1) {
				Intent intent = new Intent(context, ClubHomepageActivity.class);
				intent.putExtra("cid", id);
				startActivity(intent);
			} else if (type == 2) {
				Intent intent = new Intent(context,
						SeeOtherPeopleSpaceActivity.class);
				intent.putExtra("friend_id", id);
				startActivity(intent);
			} else if (type == 3) {
				Intent intent = new Intent(context,
						ActivityReadingActivity.class);
				intent.putExtra("actId", id);
				intent.putExtra("uid", Uid);
				startActivity(intent);
			} else if (type == 4) {
				Intent intent = new Intent(context,
						ActivityReadingActivity.class);
				intent.putExtra("actId", id);
				intent.putExtra("uid", Uid);
				startActivity(intent);
			} else if (type == 5) {
				Intent intent = new Intent(context,
						RecordAdayToRememberActivity.class);

				intent.putExtra("uid", Uid);
				startActivity(intent);
			}
		}
	}

}
